package com.seeyon.ctp.rest.resources;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.collaboration.manager.ColManager;
import com.seeyon.apps.gkcwplugin.dao.DealDataDao;
import com.seeyon.apps.gkcwplugin.po.ChailfBankPO;
import com.seeyon.apps.gkcwplugin.po.FukuanBankPO;
import com.seeyon.apps.gkcwplugin.po.HetongBankPO;
import com.seeyon.apps.gkcwplugin.po.NeibuZjBankPO;
import com.seeyon.apps.gkcwplugin.util.BaseConnection;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.affair.CtpAffair;
import com.seeyon.ctp.util.json.JSONUtil;

@Path("receiveData")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces(MediaType.APPLICATION_JSON)
public class ReceiveDataResource extends BaseResource {
	
	private DealDataDao dealDataDao = (DealDataDao) AppContext.getBean("dealDataDao");
	
	private static final Log logger = LogFactory.getLog(ReceiveDataResource.class);
	
	@SuppressWarnings("unchecked")
	@POST
	@Produces({ MediaType.APPLICATION_JSON })
	@Path("/getData")
	public Response getData(ArrayList<Map<String, Object>> params) throws BusinessException{
		Connection conn = BaseConnection.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			for (Map<String, Object> param : params) {
				String formmainId = (String) param.get("OAZJB_ZB_ID");
				String formType = (String) param.get("OAZJB_ZB_BDLX");
				String voucherNo = (String) param.get("voucherNo");
				
				String sql = "SELECT * FROM OAZJB_HTXX WHERE OAZJB_HTXX_ID = ?";
			
				ps = conn.prepareStatement(sql);
				ps.setLong(1, Long.parseLong(formmainId));
				rs = ps.executeQuery();
				while (rs.next()) {
					if ("1".equals(formType)) {
						FukuanBankPO fkPO = new FukuanBankPO();
						fkPO.setId(Long.parseLong(rs.getString("OAZJB_HTXX_MXID")));
						fkPO.setFormmainId(Long.parseLong(formmainId));
						fkPO.setFkzh(rs.getString("OAZJB_HTXX_FKZH"));
						fkPO.setPzh(voucherNo);
						
						dealDataDao.updateFukuanBankPOData(fkPO);
					} else if ("2".equals(formType)) {
						ChailfBankPO clPO = new ChailfBankPO();
						clPO.setId(Long.parseLong(rs.getString("OAZJB_HTXX_MXID")));
						clPO.setFormmainId(Long.parseLong(formmainId));
						clPO.setFkzh(rs.getString("OAZJB_HTXX_FKZH"));
						clPO.setPzh(rs.getString("OAZJB_HTXX_PZH"));
						
						dealDataDao.updateChailfBankPOData(clPO);
					} else if ("3".equals(formType)) {
						HetongBankPO htPO = new HetongBankPO();
						htPO.setId(Long.parseLong(rs.getString("OAZJB_HTXX_MXID")));
						htPO.setFormmainId(Long.parseLong(formmainId));
						htPO.setFkzh(rs.getString("OAZJB_HTXX_FKZH"));
						htPO.setPzh(rs.getString("OAZJB_HTXX_PZH"));
						
						dealDataDao.updateHetongBankPOData(htPO);
					} else if ("4".equals(formType)) {
						NeibuZjBankPO nbzjPO = new NeibuZjBankPO();
						nbzjPO.setId(Long.parseLong(rs.getString("OAZJB_HTXX_MXID")));
						nbzjPO.setFormmainId(Long.parseLong(formmainId));
						nbzjPO.setFkzh(rs.getString("OAZJB_HTXX_FKZH"));
						nbzjPO.setPzh(rs.getString("OAZJB_HTXX_PZH"));
						
						dealDataDao.updateNeibuZjBankPOData(nbzjPO);
					} else if ("5".equals(formType)) {
						
					} else if ("6".equals(formType)) {
//						ZiJinRZBankPO zjrzPO = new ZiJinRZBankPO();
//						zjrzPO.setId(Long.parseLong(rs.getString("OAZJB_HTXX_MXID")));
//						zjrzPO.setFormmainId(Long.parseLong(formmainId));
//						zjrzPO.setFkzh(rs.getString("OAZJB_HTXX_FKZH"));
//						zjrzPO.setPzh(rs.getString("OAZJB_HTXX_PZH"));
//						
//						dealDataDao.updateZiJinRZBankPOData(zjrzPO);
					} else if ("7".equals(formType)) {
						
					} else {
						logger.error("表单类型错误，无法回填数据");
					}
				}
			
			}
		} catch (SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				ps.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}

}
